#include <iostream>
#include <fstream>
#include "src/Config.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TLatex.h"
#include "TFile.h"
#include "TLine.h"
#include "TSpectrum.h"
#include <vector> 
#include <sys/stat.h>


using namespace std;
#include "src/PMTtest.cc"
#include "src/ProcessWave.cc"
#include "src/ProcessFile.cc"

//int file_exists(const char *filename)
// {
//    return (access(filename, 0) == 0);
// }

//configure:
//mainpulse_slection
////wave lenght=21000
//
////pointers
////vector to save all the ap
////pp_
//processonewave()
////fill the ROOT files, for each wave, find out the main pulse info, after pulses info
//// suppose 5 maximum afterpulses
////branch1- main pulse total_ap
////branch2-ap1
////branch3-ap2
////branch4-ap3
////branch5-ap3
Int_t main(int argc,char  ** argv)
{
    const char * path = "/home/pmthome/aptest/raw2root/";
    const char * date = "08-25-0738";
    const char * PMTid = "EA6068";
    const char * daq = "8830";
    const char * gcuid = "0";
    const char * gcusubid = "1";
    const char * hv = "1801";
    const char * sn = "EA6068";
	int frame_length=10008;

if (argc ==1){}             //do nothing
 else                    //read parameters as filenames
     {
    for(int i=1;i<argc;i++)
      {   if(strcmp(argv[i],"-s")== 0){i++;PMTid=argv[i];}      
    else if(strcmp(argv[i],"-d")== 0){i++;date=argv[i];}   
    else if(strcmp(argv[i],"-a")== 0){i++;daq=argv[i];}   
    else if(strcmp(argv[i],"-g")== 0){i++;gcuid=argv[i];}   
    else if(strcmp(argv[i],"-gs")== 0){i++;gcusubid=argv[i];}   
    else if(strcmp(argv[i],"-hv")== 0){i++;hv=argv[i];}   
    else if(strcmp(argv[i],"-sn")== 0){i++;sn=argv[i];}   
      }
     }
//    const char * date = "20190427";
//    const char * PMTid = "PA1712-1657";
//        string outputFullFilePath = string("/home/pmthome/aptest/Resultsroot/APTest_") + string(date) + string("_") + string(PMTid) + string("_") + to_string(1) + string("_OUT.root");
        string inputFullFilePath = string("/pmtfs/disk02/container_data/container4/test/FADC_Run/") + string(daq) + string("/") + string(gcuid)+string("_")+string(gcusubid) + string("_") +string(date)+string("_")+ to_string(frame_length) + string("_")+string(daq); 
		
if(file_exists(inputFullFilePath.c_str())){cout<<"file ok!"<<inputFullFilePath<<endl;}
string dir=string("Analysis_Results/") + string(daq);
if(file_exists(dir.c_str())){cout<<"dir existed!"<<dir<<endl;}
else{
const int dir_err = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (-1 == dir_err)
{
	    printf("Error creating directory!n");
		    exit(1);
}
}
//if(file_exists(outputFullFilePath.c_str())){cout<<"file generated!"<<endl;}
//else{
    ProcessOneFile(path,date,gcuid,gcusubid,daq,sn);
//}
//    NNVTPostAnalysisProcess(date, PMTid);
//   Drawwaveform(date, PMTid);
//   Apanalysis(date,PMTid);
    return 0;
}
